package com.qdairlines.service.file;
import java.io.IOException;

import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.qdairlines.common.TraceUtil;
/**
 * @author pangyannan
 * @date 2016年8月9日
 * @company QingDao Airlines
 * @description   FastDFS 的连接池
 * @version V2.0
 */
public class FastDFSConnectionPool {
	
	private static final Logger logger = LoggerFactory.getLogger(FastDFSConnectionPool.class);
	//配置文件路径
	private static final String CONF_FILENAME = FastDFSConnectionPool.class .getClassLoader().getResource( "//").getPath()+ "fdfs_client.conf" ;
	
	//最大连接数,可以写配置文件
    private static final int MAX_SIZE = 100;
    //已用连接数
    private static int busy_size = 0;  
    //最大等待时间
    private static final int MAX_WAITTIME = 10000;
    //默认等待时间
    private static final int WAIT_TIME = 1000;
    
    static {
    	 new FastDFSConnectionPool();
    }
    
    //默认获取连接等待时间为10
    public static StorageClient1 checkout(){
    	return checkout(WAIT_TIME);
    }
    
    //取出连接
    public static StorageClient1 checkout(int waittime){
    	if(waittime>=MAX_WAITTIME){
    		return null;
    	};
        StorageClient1 storageClient1 = null;
        //获取连接
        if(busy_size<=MAX_SIZE){
        	storageClient1 = getStorageClient1();
        	busy_size++;
        }else{
        	 try {
				Thread.sleep(WAIT_TIME);
				waittime = waittime + WAIT_TIME;
				checkout(waittime);
			} catch (InterruptedException e) {
				  TraceUtil.error(logger, e);
			}                 
        }
       
        
        return storageClient1;
    }
    public static void checkin(StorageClient1 storageClient1){
    	storageClient1 = null;
    	busy_size --;
    }
    
    //获取一个连接
    private static StorageClient1 getStorageClient1(){
        TrackerServer trackerServer = null;
        StorageClient1 storageClient1 = null;
        try {
            TrackerClient trackerClient = new TrackerClient();
            //只需要一个tracker server连接
            trackerServer = trackerClient.getConnection();
            StorageServer storageServer = null;          

            storageClient1 = new StorageClient1(trackerServer,storageServer);
            
            
        } catch (IOException e) {
        	 TraceUtil.error(logger, e);
        }finally{
            if(trackerServer != null){
                try {
                    trackerServer.close();
                } catch (IOException e) {
                	 TraceUtil.error(logger, e);
                }
            }
        }
        return storageClient1;
    }   

    //单例
    private  FastDFSConnectionPool(){
        init();
    }
    
    //初始化连接池
    private void init(){
        initClientGlobal();
    }
    //初始化客户端
    private void initClientGlobal(){
    	//通过配置文件初始化
    	try {
			ClientGlobal.init(CONF_FILENAME); 
		} catch (Exception e) {
			TraceUtil.error(logger, e);
		}
    }

}
